﻿using System;
using System.Linq.Expressions;
using Skynet.CommonLibrary.Extension.Serialize;

namespace Skynet.CommonLibrary.WCF
{
    public class DataCenterBase
    {
        protected T GetService<T>(string className, string methodName, string userName, object[] values)
        {
            if (IsBaseType(typeof(T)))
            {
                return (T)DataCenterOperator.Instance.GetObject(new ParamesEntity
                                                                    {
                                                                        ClassName = className, 
                                                                        MethodName = methodName, 
                                                                        UserName = userName, 
                                                                        Values = GetServiceObjs(values)
                                                                    });
            }
            byte[] data = DataCenterOperator.Instance.FindData(new ParamesEntity
                                                                   {
                                                                       ClassName = className, 
                                                                       MethodName = methodName, 
                                                                       UserName = userName, 
                                                                       Values = GetServiceObjs(values)
                                                                   });
            if (data != null)
            {
                return (T)data.DeserializeData<Object>();
            }
            return default(T);
        }
        protected T GetServiceExpress<T, TEntity>(string className, string methodName, string userName, object[] values, Expression<Func<TEntity, bool>> express)
            where TEntity : class
            where T : class
        {
            byte[] data = DataCenterOperator.Instance.FindEntity(new ParamesEntity { ClassName = className, MethodName = methodName, UserName = userName, Values = GetServiceObjs(values) }, express);
            return data != null ? data.DeserializeData<T>() : null;
        }
        private bool IsBaseType(Type type)
        {
            switch (type.ToString())
            {
                case "System.Int32":
                case "System.String":
                case "System.Double":
                case "System.Decimal":
                case "System.Collections.Hashtable":
                case "System.Collections.ArrayList":
                    return true;
                default:
                    return false;
            }
        }
        private object[] GetServiceObjs(object[] objArray)
        {
            if (objArray == null)
            {
                return null;
            }
            object[] objs = new object[objArray.Length];
            for (int i = 0; i < objArray.Length; i++)
            {
                Type objType = objArray[i].GetType();
                if (objType.IsValueType ||
                    objType.ToString() == "System.String" ||
                    objType.ToString() == "System.Collections.Generic.Dictionary" ||
                    objType.ToString() == "System.Collections.Hashtable" ||
                    objType.ToString() == "System.Byte[]" ||
                    objType.ToString() == "System.Collections.ArrayList")
                {
                    objs[i] = objArray[i];
                }
                else
                {
                    if (objType.IsSerializable)
                    {
                        objs[i] = objArray[i].SerializeData();
                    }
                    else
                    {
                        objs[i] = objArray[i];
                    }
                }
            }
            return objs;
        }
    }
}
